// pages/personData/personData.js
const app = getApp()
const wh = require("../../utils/wh.js");
import WxValidate from "../../utils/WxValidate.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    mobile: '',
    housename: '',
    num0: '',
    num1: '',
    auth_modal: false, // 授权窗口
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    avatar: '../../images/head.png'
  },
  // 表单提交
  bindsubmit: function(e) {
    let rules = {
      username: {
        required: true,
      },
      mobile: {
        required: true,
        tel: true,
      },
      housename: {
        required: true,
      },
      num0: {
        required: true,
        digits: true,
      },
      num1: {
        required: true,
        digits: true,
      },
    }
    // 验证字段的提示信息，若不传则调用默认的信息  
    let messages = {
        username: {
          required: '请输入真实姓名',
        },
        mobile: {
          required: '请输入手机号码',
          tel: '请输入正确的手机号码',
        },
        housename: {
          required: '请输入小区名称',
        },
        num0: {
          required: '请输入楼栋号',
          digits: '栋号只能输入数字',
        },
        num1: {
          required: '请输入房间号',
          digits: '房间号只能输入数字',
        },
      },
      Validate = new WxValidate(rules, messages);
    var that = this;
    var formData = e.detail.value;
    if (!Validate.checkForm(e)) {
      const error = Validate.errorList[0]
      //提示信息  
      wx.showToast({
        title: error.msg,
        icon: 'none'
      });
      return false
    } else {
      let postUrl = '/api/madetoorder.decorate.index/get_operate_user_info',
        that = this,
        params = {
          username: formData.username,
          mobile: formData.mobile,
          court: formData.housename,
          build: formData.num0,
          number: formData.num1,
          is_submit: 1
        };
      wh.Post(postUrl, params, function(res) {
        console.log(res)
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000,
          complete: function() {
            setTimeout(function () {
              wx.switchTab({
                url: '../index/index'
              })
            }, 2000);
          }
        });
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  // 取消授权
  cancel_auth: function() {
    wx.switchTab({
      url: '../index/index'
    });
    this.setData({
      auth_modal: false
    })
  },
  /**
   * 确认授权
   */
  bindGetUserInfo: function(e) {
    let userInfo = e.detail,
      that = this;
    this.setData({
      userInfo: userInfo
    });
    if (e.detail.userInfo) {
      wx.showToast({
        title: '授权成功',
      })
      setTimeout(function() {
        let auth_rs = app.wlogin(userInfo);
        let employId = wx.getStorageSync('employId');
        if (!employId || employId == '') {
          app.employIdCallback = employId => {
            if (employId != '') {
              that.setData({
                auth_modal: false,
                username: userInfo.userInfo.nickName,
                avatar: userInfo.userInfo.avatarUrl
              })
            }
          }
        }
      }, 1500)
    } else {
      wx.switchTab({
        url: '../index/index'
      })
    }
  },
  onLoad: function(options) {
    let userInfoStr = wx.getStorageSync('W_Userinfo');
    if (userInfoStr != '' && userInfoStr != undefined) {
      let userInfo = JSON.parse(wx.getStorageSync('W_Userinfo'));
      if (userInfo != '') {
        this.setData({
          'username': userInfo.nickname,
          'avatar': userInfo.avatar
        })
      }
    }
    let postUrl = '/api/madetoorder.decorate.index/get_operate_user_info',
      that = this;
    wh.Post(postUrl, { is_submit: 0}, function (res) {
      if (res != -1) {
        that.setData({
          'username': res.username,
          'mobile': res.mobile,
          'housename': res.court,
          'num0': res.build,
          'num1': res.number
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    wx.getExtConfig({
      success: function(res) {
        module.exports.globalData = res.extConfig;
        let s_app_id = wx.getStorageSync('whlw_app_id');
        if (s_app_id != module.exports.globalData.app_id) {
          that.setData({
            auth_modal: true
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})